package com.server.questionbank.snframe.service.NoteService.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.server.questionbank.snframe.domain.Note.UserAttachment;
import com.server.questionbank.snframe.mapper.Note.UserAttachmentMapper;
import com.server.questionbank.snframe.service.NoteService.UserAttachmentService;
import org.springframework.stereotype.Service;
import jakarta.annotation.Resource;
import java.util.List;

@Service
public class UserAttachmentServiceImpl extends ServiceImpl<UserAttachmentMapper, UserAttachment> implements UserAttachmentService {
    
    @Resource
    private UserAttachmentMapper userAttachmentMapper;
    
    @Override
    public List<UserAttachment> listByUserId(Long userId) {
        return userAttachmentMapper.findByUserId(userId);
    }
    
    @Override
    public UserAttachment getByIdAndUserId(Long attachmentId, Long userId) {
        return this.lambdaQuery()
            .eq(UserAttachment::getAttachmentId, attachmentId)
            .eq(UserAttachment::getUserId, userId)
            .one();
    }
    
    @Override
    public Float getMaxOrderByUserId(Long userId) {
        UserAttachment maxOrder = this.lambdaQuery()
            .eq(UserAttachment::getUserId, userId)
            .orderByDesc(UserAttachment::getAttachmentOrder)
            .last("LIMIT 1")
            .one();
        return maxOrder != null ? maxOrder.getAttachmentOrder() : null;
    }
    
    @Override
    public UserAttachment getByUrlAndUserId(String url, Long userId) {
        return this.lambdaQuery()
            .eq(UserAttachment::getAttachmentUrl, url)
            .eq(UserAttachment::getUserId, userId)
            .one();
    }
} 